/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.tools;

import application.model.GraphEntity;
import application.model.GraphEntityAccomps;
import application.model.GraphEntityKeywords;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author aa6345
 */
public class ReportTool {

    private String entityAdditionalDataXML = "";

    public String createSearchReportAndDetailsXML(
            HashMap<String, GraphEntity> formatedSearchResults,
            String searchTerm,
            HashMap<String,String> centralNode) {
        return "Method not used";
    }

    /*Creates search report and
     *XML for additional data (used when a node is clicked on the graph to
     *   display matched keywords and accomplishments in side box)
     * centralNode attribs : name, id,
     */
    public String createSearchReportAndDetailsXML(
        HashMap<Long, GraphEntity> formatedSearchResults,
        HashMap<Long, GraphEntityAccomps> searchResults2graphEntityAccomps,
        HashMap<Long, GraphEntityKeywords> searchResults2graphEntityKeywords,
        String searchTerm,
        HashMap<String,String> centralNode) {
        String fullReport = "";
        String listOfKeywords = "";
        String listOfEntities="";
        String listOfMatchedItems = "";
        String csvExport = "";
        String entityAdditionalDataXMLloc = "<entities>";

        ArrayList<String> uniqueListOfKeywords = new ArrayList<String>();
        HashMap<String,ArrayList<String>> tmpListOfKeywordsKeyAccompId = new HashMap<String,ArrayList<String>>();

        if(centralNode == null || centralNode.isEmpty()) {
            centralNode.put("name", searchTerm.replace("'", ""));
            centralNode.put("id", "001");
        }

        for (Long entityIDkey : formatedSearchResults.keySet()) {

            String entityName="";
            String entityEmail="";
            String kwrdsOfEntityDetailsXML = "";

            if(formatedSearchResults.get(entityIDkey).getEntityFName() != null ) {
                entityName += formatedSearchResults.get(entityIDkey).getEntityFName() + " ";
            }
            if(formatedSearchResults.get(entityIDkey).getEntityMName() != null ) {
                entityName += formatedSearchResults.get(entityIDkey).getEntityMName() + " ";
            }
            if(formatedSearchResults.get(entityIDkey).getEntitySName() != null ) {
                entityName += formatedSearchResults.get(entityIDkey).getEntitySName() + " ";
            }
            if(formatedSearchResults.get(entityIDkey).getEntityEmail() != null ) {
                entityEmail += formatedSearchResults.get(entityIDkey).getEntityEmail().replace("'", "") + " ";
            }
            listOfEntities += entityName + " , " + entityEmail + "<br/>";

            String thisEntityKwrdAccompList = "";

            //create listOfMatchedItems. Format:
            //- entity name "[" + keyword + " in INTERESTS ]" + accompTitle + "<br/>";
            if(searchResults2graphEntityAccomps.containsKey(entityIDkey)) {
                if(!searchResults2graphEntityAccomps.get(entityIDkey).getGea().isEmpty()) {
                    for(Long entAccompID : searchResults2graphEntityAccomps.get(entityIDkey).getGea().keySet()) {
                        listOfMatchedItems += "- "+entityName.replace("'", "");
                        listOfMatchedItems += " [";

                        for(String entKeyWord : searchResults2graphEntityKeywords.get(entityIDkey).getGek().keySet()) {
//System.out.println("%%%%% the keyword is: "+entKeyWord);
                            if(searchResults2graphEntityKeywords.get(entityIDkey).getGek().get(entKeyWord) == null) {
                            } else {
                                if(searchResults2graphEntityKeywords.get(entityIDkey).getGek().get(entKeyWord).contains(entAccompID)) {
                                    listOfMatchedItems += entKeyWord+", ";
                                    //create CSV export
                                    //format keyword,accomplishment [AccompType],matchedPerson, matchedPerson email
                                    csvExport += entKeyWord+", ";
                                    csvExport += searchResults2graphEntityAccomps.get(entityIDkey).getGea().get(entAccompID).getAccompTitle().replace("'", "");
                                    csvExport += " [";
                                    csvExport += searchResults2graphEntityAccomps.get(entityIDkey).getGea().get(entAccompID).getAccomplishmentType().replace("'", "");
                                    csvExport += " ], ";
                                    csvExport += entityName+","+entityEmail+"<br/>";
                                    kwrdsOfEntityDetailsXML += "<keyword>";
                                    kwrdsOfEntityDetailsXML += "<keywordname>" + entKeyWord.replace("'", "") + "</keywordname>";
                                    kwrdsOfEntityDetailsXML += "<accomptitle>" + searchResults2graphEntityAccomps.get(entityIDkey).getGea().get(entAccompID).getAccompTitle().replace("'", "") + "</accomptitle>";
                                    kwrdsOfEntityDetailsXML += "<accomptype>" + searchResults2graphEntityAccomps.get(entityIDkey).getGea().get(entAccompID).getAccomplishmentType().replace("'", "") + "</accomptype>";
                                    kwrdsOfEntityDetailsXML += "</keyword>";
                                }
                            }
                        }
                        listOfMatchedItems += " in ";
                        listOfMatchedItems += searchResults2graphEntityAccomps.get(entityIDkey).getGea().get(entAccompID).getAccomplishmentType().toUpperCase().replace("'", "");
                        listOfMatchedItems += "] "+searchResults2graphEntityAccomps.get(entityIDkey).getGea().get(entAccompID).getAccompTitle().replace("'", "");
                        listOfMatchedItems += " <br/>";

                    }
                }
            }
            entityAdditionalDataXMLloc += "<entity>";
            entityAdditionalDataXMLloc += "<id>" + entityIDkey + "</id>";
            entityAdditionalDataXMLloc += "<name>" + entityName.replace("'", "") + "</name>";
            entityAdditionalDataXMLloc += "<keywords>";
            entityAdditionalDataXMLloc += kwrdsOfEntityDetailsXML;
            entityAdditionalDataXMLloc += "</keywords>";
            entityAdditionalDataXMLloc += "<detailslink>" + entityIDkey + "</detailslink>";
            entityAdditionalDataXMLloc += "</entity>";
        }

        //add all keywords
        for(Long keywordEntID:searchResults2graphEntityKeywords.keySet()) {
            for(String keyword : searchResults2graphEntityKeywords.get(keywordEntID).getGek().keySet()) {
                if(!uniqueListOfKeywords.contains(keyword.toLowerCase())) {
                    uniqueListOfKeywords.add(keyword.toLowerCase());
                    listOfKeywords += "<br/>"+keyword.toLowerCase();
                }
            }
            //System.out.println("***"+entityName+"my kewords are: "+keyword.toLowerCase()+"");
        }

        //add all unique keywords to the report
        /*
        for(int kwrdid=0; uniqueListOfKeywords.size()>kwrdid; kwrdid++) {
            if(uniqueListOfKeywords.get(kwrdid) != null) {
                listOfKeywords += "<br/>"+uniqueListOfKeywords.get(kwrdid);
            }
        }
*/
        //csv export
        //sTemp.getKeyword() + "," + tmpItem + "[" + sTemp.getDataSource() + "]" + "," + usrTmp.getFirstName() + " " + usrTmp.getSurname() + "," + usrTmp.getEmailAddress() + "\r\n";


        if(listOfEntities.length()<1) {
            fullReport = "Report not generated!";
        } else {
            fullReport = "<h2>Search Report</h2> <strong><a name=\"backToTop\">Search term:</a> "+searchTerm+"</strong>";
            fullReport += "<p>";
            fullReport += "<a href=\"#searchKeywords\">Search Keywords</a><br/>";
            fullReport += "<a href=\"#entities\">Entity - Person List</a><br/>";
            fullReport += "<a href=\"#keywordMatch\">Keyword/Term Match Details</a><br/>";
            fullReport += "<a href=\"#csvExport\">CSV Export</a>";
            fullReport += "</p>";
            fullReport += "<p><strong><a name=\"searchKeywords\">Search Keywords</a></strong></p><br/>";
            fullReport += listOfKeywords;
            fullReport += "<p><strong><a href=\"#backToTop\">back to top</a></strong></p><br/>";
            fullReport += "<p><strong><a name=\"entities\">Entities - Persons</a></strong></p><br/>";
            fullReport += listOfEntities;
            fullReport += "<p><strong><a href=\"#backToTop\">back to top</a></strong></p><br/>";
            fullReport += "<p><strong><a name=\"keywordMatch\">Keyword/Term Match Details</a></strong></p><br/>";
            fullReport += listOfMatchedItems;
            fullReport += "<p><strong><a href=\"#backToTop\">back to top</a></strong></p><br/>";
            fullReport += "<p><strong><a name=\"csvExport\">CSV Export</a></strong></p><br/>";
            fullReport += csvExport;
            fullReport += "<p><strong><a href=\"#backToTop\">back to top</a></strong></p><br/>";
        }

        //Entity Central Node details XML
        entityAdditionalDataXMLloc += "<entity>";
        entityAdditionalDataXMLloc += "<id>001</id>";
        entityAdditionalDataXMLloc += "<name>" +centralNode.get("name").replace("'", "")+ "</name>";
        entityAdditionalDataXMLloc += "<keywords>";
        entityAdditionalDataXMLloc += "<keyword>";
        entityAdditionalDataXMLloc += "<keywordname>See Details section</keywordname>";
        entityAdditionalDataXMLloc += "<accomptitle>_</accomptitle>";
        entityAdditionalDataXMLloc += "<accomptype>_</accomptype>";
        entityAdditionalDataXMLloc += "</keyword>";
        entityAdditionalDataXMLloc += "</keywords>";
        //ID in detailslink tag is used to create More Details link
        entityAdditionalDataXMLloc += "<detailslink>"+centralNode.get("id")+"</detailslink>";
        entityAdditionalDataXMLloc += "</entity>";

        //close the main tag
        entityAdditionalDataXMLloc += "</entities>";

        //Temp fix
        //remove &, â, ¦, Ã, ¯, ¿, ½, ¢, ¬, Â
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("&", "and");
        /*entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("â", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("¦", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("Ã", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("¯", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("¿", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("½", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("¢", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("¬", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("'", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("\"", "");
        entityAdditionalDataXMLloc = entityAdditionalDataXMLloc.replace("Â", "");*/

        this.setEntityAdditionalDataXML(entityAdditionalDataXMLloc);

        fullReport = fullReport.replace("'", " ");
        return fullReport;

    }

    public String getEntityAdditionalDataXML() {
        return entityAdditionalDataXML;
    }

    public void setEntityAdditionalDataXML(String entityAdditionalDataXML) {
        this.entityAdditionalDataXML = entityAdditionalDataXML;
    }
}
